﻿@page "/index"
@using BootstrapBlazor.OnlyServerZhongShun.Data.dtomodels
@attribute [TabItemOption(Text = "首页", Closable = false)]
@inject SqlSugarScope _SqlSugarScope
@using AntDesign.Charts 
@using Title = AntDesign.Charts.Title


<PageTitle>Index</PageTitle>

<AuthorizeView Roles="超级管理员, 业务总">


<div class="row g-1">
    @if (按部门统计销售额List != null)
    {
        @* var colWidth = 12 / 按部门统计销售额List.Count; // 计算应该使用的栅格宽度
        colWidth = Math.Max(colWidth, 2); // 确保最小宽度为1
        var colClass = $"col-md-{colWidth}"; // 创建列的CSS类
        var width = (100/按部门统计销售额List.Count).ToString("p0"); *@
        @foreach (var item in 按部门统计销售额List)
        {
@*             string width = "";
            if (item.部门.Length > 5)
            {
                width = "14";
            }
            else
            {
                width = "12";
            }
            <div class="" style="flex: 0 0 auto;width: @(width)%;"> *@
                <div class="col-md-2">
                <Card IsShadow="true">
                    <BodyTemplate>
                        <div class="row align-items-center no-gutters">
                            @* <div class="col-auto"><i class="fa-regular fa-calendar-check fa-2x"></i></div> *@
                            <div class="col">
                                <div class="text-primary fw-bold mb-1"><span>@item.部门</span></div>
                                <div class="text-dark fw-bold h5">
                                    <CountUp Value="@item.销售额"></CountUp>
                                </div>
                            </div>
                        </div>
                    </BodyTemplate>
                </Card>
            </div>
        }

        <div class="col-md-2">
            <Card IsShadow="true">
                <BodyTemplate>
                    <div class="row align-items-center no-gutters">
                        @* <div class="col-auto"><i class="fa-regular fa-calendar-check fa-2x"></i></div> *@
                        <div class="col">
                            <div class="text-primary fw-bold mb-1"><span>合计</span></div>
                            <div class="text-dark fw-bold h5">
                                <CountUp Value="@按部门统计销售额List.Sum(it => it.销售额)"></CountUp>
                            </div>
                        </div>
                    </div>
                </BodyTemplate>
            </Card>
        </div>
    }
    </div>
    <div class="row g-1">
    <div class="col-md-9">

        <Card IsShadow="false" HeaderText="销售额月度同期对比趋势">
           @*  <HeaderTemplate>
                <DateTimePicker ViewMode="DatePickerViewMode.Date" Value="@DateTimePickerValue">
                    <TimePickerSetting ShowClockScale="false" IsAutoSwitch="false" />
                </DateTimePicker>
            </HeaderTemplate> *@
                <BodyTemplate>
                    <div class="chart-area">
                        @* <Chart @ref="BarChart" Height="320px" Width="auto" style="display: block; width: auto; height: 320px;"
                               Chartyear="Chartyear.Line" OnInitAsync="OnInitBarChartAsync"></Chart> *@
                    <AntDesign.Charts.Line Data="同期月度销售额List" Config="config"/>
                    </div>
                </BodyTemplate>
            </Card>
     </div>
        <div class="col-md-3">
            <Card IsShadow="true" HeaderText="业务员销售排名">
                <BodyTemplate>
                    <div style="margin-right:10px;">
                        @if (业务员销售排名List != null)
                        {
                           
                            @foreach (var item in 业务员销售排名List)
                            {
                            <div class="row g-0" style="margin-top:20px;">
                                    <div class="col-md-1"><Badge Color="Color.Primary">@item.序号</Badge></div>
                                    <div class="col-md-2">@item.姓名</div>
                                    @* <label class="col-md-2">@item.姓名</label>  *@
                                    <BootstrapBlazor.Components.Progress class="col-md-6" Value="@item.Percent" IsAnimated="true" Color="@Color.Success" IsShowValue="false" Round="1"></BootstrapBlazor.Components.Progress>
                                    <div class="col-md-3">@(item.销售额)</div>
                                    @* <label class="col-md-2">@(item.销售额)万</label> *@
                                </div>  
                            }
                        }
                        
                        
                    </div>
                </BodyTemplate>
            </Card>
        </div>

      @*   <div class="col-md-9">
            <Card IsShadow="true" HeaderText="部门销售额同期对比">
                <BodyTemplate>
                <AntDesign.Charts.Column Data="部门销售额同期对比List" Config="configColumn" />
                </BodyTemplate>
            </Card>
        </div>

    <div class="col-md-3">
        <Card IsShadow="true" HeaderText="部门销售占比">
            <BodyTemplate>
                <Pie Data="部门销售占比List" Config="configPie" />
            </BodyTemplate>
        </Card>
    </div> *@
</div>
</AuthorizeView>
@code {
    private List<按部门统计销售额> 按部门统计销售额List = new List<按部门统计销售额>();
    private List<同期月度销售额> 同期月度销售额List = new List<同期月度销售额>();
    private List<业务员销售排名> 业务员销售排名List = new List<业务员销售排名>();
    private List<部门销售占比> 部门销售占比List = new List<部门销售占比>();
    private List<部门销售额同期对比> 部门销售额同期对比List = new List<部门销售额同期对比>();
    private List<(string, string)> yourItems = new List<(string, string)>();

    private DateTime DateTimePickerValue{ get; set; }

    protected override async Task OnInitializedAsync()
    {
        查按部门统计销售额();
        查同期月度销售额();
        查业务员销售排名();
        // 查部门销售额同期对比();
        // 查部门销售占比();
    }
    private void 查按部门统计销售额()
    {
        string sql = @"SELECT
                    ROW_NUMBER() OVER(ORDER BY
                        CASE
                            WHEN View_PersonDepart.DepartmentName like '一%' THEN 1
                            WHEN View_PersonDepart.DepartmentName like '二%' THEN 2
                            WHEN View_PersonDepart.DepartmentName like '三%' THEN 3
			                 WHEN View_PersonDepart.DepartmentName like '四%' THEN 4
                            ELSE 5  -- 如果有其他部门，排序在这些部门之后
                        END
                    ) AS RowNum,
                    View_PersonDepart.DepartmentName AS 部门,
                    Convert(decimal(18,4),SUM(iMoney)/10000)  AS 销售额
                FROM
                    View_发票明细
                JOIN
                    View_PersonDepart ON View_PersonDepart.cPersonCode = View_发票明细.cPersonCode
                WHERE
                    DATEDIFF(year, ddate, GETDATE()) = 0
                GROUP BY
                    View_PersonDepart.DepartmentName
                ORDER BY
                    CASE
                        WHEN View_PersonDepart.DepartmentName like '一%' THEN 1
                        WHEN View_PersonDepart.DepartmentName like '二%' THEN 2
                        WHEN View_PersonDepart.DepartmentName like '三%' THEN 3
		                WHEN View_PersonDepart.DepartmentName like '四%' THEN 4
                        ELSE 5
                    END
                    ";
        按部门统计销售额List = _SqlSugarScope.Ado.SqlQuery<按部门统计销售额>(sql);

    }

    private void 查同期月度销售额()
    {
        string sql = @"SELECT YEAR(dDate) YEAR,
                        MONTH(dDate) AS Month,Convert(decimal(18,4),SUM(iMoney)/10000) CurrentYearTotal
	                    FROM
                        View_发票明细
                    WHERE
                        YEAR(dDate) >= YEAR(GETDATE()) - 1
                    GROUP BY
                        MONTH(dDate),YEAR(dDate)
                    ORDER BY
                        YEAR(dDate),MONTH(dDate);
                    ";
        同期月度销售额List = _SqlSugarScope.Ado.SqlQuery<同期月度销售额>(sql);

    }

    private void 查业务员销售排名()
    {
        string sql = @"select top 10 ROW_NUMBER() OVER(ORDER BY sum(销售额) desc) 序号,  姓名,Convert(decimal(18,4),sum(销售额)/10000) 销售额
                        from view_业务员销售额
                        where DATEDIFF(year,dDate,getdate())=0
                        group by 姓名
                        order by 销售额 desc
                    ";
        业务员销售排名List = _SqlSugarScope.Ado.SqlQuery<业务员销售排名>(sql);
        if (业务员销售排名List != null && 业务员销售排名List.Count > 0)
        {
            double max = 业务员销售排名List.Max(x => x.销售额);
            foreach (var item in 业务员销售排名List)
            {
                item.Percent = (item.销售额 / max) * 100;
            }
        }
    }
    private void 查部门销售额同期对比()
    {
            string sql = @"SELECT YEAR(dDate) year,
        View_PersonDepart.DepartmentName department,
        Convert(decimal(18,4),SUM(iMoney)/10000) iMoney
                    FROM
                    View_发票明细
        join View_PersonDepart on View_PersonDepart.cPersonCode = View_发票明细.cPersonCode
                WHERE
                    YEAR(dDate) >= YEAR(GETDATE()) - 1
                GROUP BY
                   YEAR(dDate),View_PersonDepart.DepartmentName
                ORDER BY
                    YEAR(dDate), View_PersonDepart.DepartmentName
                        ";
        // string sql = @"SELECT [事业部] department
        //       ,[年度] year
        //       ,[销售]/10000 iMoney
        //   FROM [netcoredev].[dbo].[View_事业部年度销售]
        // order by [部门] desc";
        部门销售额同期对比List = _SqlSugarScope.Ado.SqlQuery<部门销售额同期对比>(sql);
        if (部门销售额同期对比List != null && 部门销售额同期对比List.Count > 0)
        {
            //根据部门分组
            var group = 部门销售额同期对比List.GroupBy(x => x.department);
            foreach (var item in group)
            {
                var data = item.ToList();
                if (data.Count > 1)
                {
                    //计算差额，并新建一个新对象塞入list
                    var diff = Math.Abs(data[0].iMoney - data[1].iMoney);
                    var newItem = new 部门销售额同期对比
                    {
                            department = data[0].department,
                            iMoney = diff,
                            year = "差异"
                    };
                    部门销售额同期对比List.Add(newItem);
                }
            }
        }
    }

    static double sum部门销售占比 = 0;
    private void 查部门销售占比()
    {
        string sql = @"select  View_PersonDepart.DepartmentName departmentName,Convert(decimal(18,4),sum(iMoney)/10000) iMoney
                    from View_发票明细
                    join View_PersonDepart on View_PersonDepart.cPersonCode = View_发票明细.cPersonCode
                    where DATEDIFF(year,ddate,getdate())=0
                    group by  View_PersonDepart.DepartmentName
                    order by iMoney desc
                    ";
        部门销售占比List = _SqlSugarScope.Ado.SqlQuery<部门销售占比>(sql);
        sum部门销售占比 = 部门销售占比List.Sum(x => x.iMoney);
        foreach (var item in 部门销售占比List)
        {
            item.percent = (item.iMoney / sum部门销售占比) * 100;
            // item.total = sum部门销售占比;
        }

    }
    LineConfig config = new LineConfig()
        {
            Title = new AntDesign.Charts.Title
            {
                Visible = true,
                Text = "销售额月度同期对比趋势"
            },

            Padding = "auto",
            ForceFit = true,
            XField = "month",
            YField = "currentYearTotal",
            Legend = new Legend
            {
                Position = "right-top"
            },
            SeriesField = "year",
            Point = new LineViewConfigPoint
            {
                Visible = true
            },

        };

    //饼图配置
    PieConfig configPie = new PieConfig()
        {
            AppendPadding = 10,
            InnerRadius = 0.6,
            Radius = 0.8,
            Padding = "auto",
            AngleField = "iMoney",
            ColorField = "departmentName",
            //设置合计值
            Meta = new
            {
                value = new
                {
                    formatter = $"(val) => {sum部门销售占比}万元",
                },
            },
        };

    ColumnConfig configColumn = new ColumnConfig()
        {
            IsGroup = true,
            XField = "department",
            YField = "iMoney",
            YAxis = new ValueAxis()
            {
                Min = 0,
            },
            Label = new ColumnViewConfigLabel()
            {
                Visible = true,
            },
            SeriesField = "year",
            Color = new string[] { "#1ca9e6", "#f88c24" },
        };

}
